package com.woshengqile.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.woshengqile.model.VideoInfoVideoInfoTag;
import com.woshengqile.service.VideoInfoCategoryService;

import cn.hutool.core.collection.CollectionUtil;
import net.hasor.core.Inject;

@ControllerBind(controllerKey = "/searchCategory")
public class SearchCategory extends BaseController {
	
	@Inject
	private VideoInfoCategoryService videoCategoryService;
	
	public void index() {
		String categorySql = "SELECT id, `name` FROM video_tag";
		List<Record> videoCategoryList = Db.find(categorySql);
		Map<String, Long> m_videoCategoryList = new HashMap<>();
		for (Record record : videoCategoryList) {
			m_videoCategoryList.put(record.getStr("name"), record.getLong("id"));
		}
		
		
		String sql = "SELECT id,tag_list FROM video_info";
		List<Record> list = Db.find(sql);
		if (CollectionUtil.isNotEmpty(list)) {
//			Set<String> set = new HashSet<>();
//			for (Record record : list) {
//				String str = record.get("tag_list");
//				String[] strArr = str != null && str.length() > 0 ? strArr = str.split(",") : new String[0];
//				for (String string : strArr) {
//					set.add(string);
//				}
//			}
//			for (String string : set) {
//				System.out.println(string);
//				VideoTag videoTag = new VideoTag();
//				
//				videoTag.setId(UidGenerator.nextId());
//				videoTag.setCreatedDate(new Date());
//				videoTag.setLastModifiedDate(new Date());
//				videoTag.setVersion(0L);
//				videoTag.setName(string);
//				videoTag.save();
//			}
//			renderJson(set);
			
			for (Record record : list) {
				String str = record.get("tag_list");
				String[] strArr = str != null && str.length() > 0 ? strArr = str.split(",") : new String[0];
				for (String string : strArr) {
					Long id = m_videoCategoryList.get(string);
					if (id != null) {
						VideoInfoVideoInfoTag v = new VideoInfoVideoInfoTag();
						v.setVideoInfoId(record.getLong("id"));
						v.setVideoInfoTagId(id);
						v.save();
					}
				}
			}
		}
		
//		String categorySql = "SELECT id, `name` FROM video_category";
//		List<Record> videoCategoryList = Db.find(categorySql);
//		Map<String, Long> m_videoCategoryList = new HashMap<>();
//		for (Record record : videoCategoryList) {
//			m_videoCategoryList.put(record.getStr("name"), record.getLong("id"));
//		}
//		
//		
//		String sql = "SELECT id, category_list FROM video_info";
//		List<Record> list = Db.find(sql);
//		if (CollectionUtil.isNotEmpty(list)) {
//			for (Record record : list) {
//				String str = record.get("category_list");
//				String[] strArr = str != null && str.length() > 0 ? strArr = str.split(",") : new String[0];
//				for (String string : strArr) {
//					Long id = m_videoCategoryList.get(string);
//					if (id != null) {
//						VideoInfoVideoCategory v = new VideoInfoVideoCategory();
//						v.setVideoInfoId(record.getLong("id"));
//						v.setVideoCategoryId(id);
//						v.save();
//					}
//				}
//			}
//		}
		renderJson("OK!");
	}
	
}
